<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>碳排放计算器</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f4f4f4;
            margin: 0;
            padding: 0;
        }

        .container {
            max-width: 600px;
            margin: 50px auto;
            padding: 20px;
            background-color: #fff;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
        }

        h1, h2 {
            text-align: center;
        }

        .form-group {
            display: flex;
            align-items: center;
            margin-bottom: 15px;
        }

        label {
            flex: 1;
            margin-right: 10px;
            text-align: right;
        }

        input {
            flex: 2;
            padding: 8px;
            box-sizing: border-box;
        }

        button {
            display: block;
            width: 100%;
            padding: 10px;
            background-color: #28a745;
            color: #fff;
            border: none;
            cursor: pointer;
            margin-top: 10px;
        }

        button:hover {
            background-color: #218838;
        }

        #result {
            margin-top: 20px;
            font-weight: bold;
            text-align: center;
        }

        #historyList {
            list-style-type: none;
            padding: 0;
            margin: 0;
        }

        #historyList li {
            margin-bottom: 5px;
            padding: 5px 0;
            border-bottom: 1px solid #ddd;
            cursor: pointer;
        }

        #historyList li:hover {
            background-color: #f8f9fa;
        }

        .buttons {
            display: flex;
            gap: 10px;
            margin-top: 20px;
        }

        .buttons button {
            flex: 1;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>碳排放计算器</h1>
        <form id="carbonForm">
            <div class="form-group">
                <label for="processEmission">工艺排放 (万吨):</label>
                <input type="number" id="processEmission" required>
            </div>
            <div class="form-group">
                <label for="energyConsumption">能源消耗 (千度):</label>
                <input type="number" id="energyConsumption" required>
            </div>
            <div class="form-group">
                <label for="resourceConsumption">资源消耗 (吨):</label>
                <input type="number" id="resourceConsumption" required>
            </div>
            <div class="form-group">
                <label for="processFactor">工艺排放因子:</label>
                <input type="number" id="processFactor" value="2.2" required>
            </div>
            <div class="form-group">
                <label for="energyFactor">能源消耗因子:</label>
                <input type="number" id="energyFactor" value="0.56" required>
            </div>
            <div class="form-group">
                <label for="resourceFactor">资源消耗因子:</label>
                <input type="number" id="resourceFactor" value="0.33" required>
            </div>
            <button type="button" onclick="calculateCarbon()">计算碳排放</button>
        </form>
        <div id="result"></div>

        <h2>历史记录</h2>
        <ul id="historyList"></ul>

        <div class="buttons">
            <button onclick="clearHistory()">清除历史</button>
            <button onclick="saveHistory()">保存历史</button>
        </div>
    </div>

    <script>
        let history = [];

        function calculateCarbon() {
            const processEmission = parseFloat(document.getElementById('processEmission').value);
            const energyConsumption = parseFloat(document.getElementById('energyConsumption').value);
            const resourceConsumption = parseFloat(document.getElementById('resourceConsumption').value);
            const processFactor = parseFloat(document.getElementById('processFactor').value);
            const energyFactor = parseFloat(document.getElementById('energyFactor').value);
            const resourceFactor = parseFloat(document.getElementById('resourceFactor').value);

            const totalCarbon = (processEmission * processFactor) + (energyConsumption * energyFactor) + (resourceConsumption * resourceFactor);

            document.getElementById('result').innerText = `总碳排放量: ${totalCarbon.toFixed(2)} 吨`;

            const timestamp = new Date().toLocaleString();
            const record = {
                time: timestamp,
                inputs: { processEmission, energyConsumption, resourceConsumption, processFactor, energyFactor, resourceFactor },
                result: totalCarbon
            };

            history.unshift(record);
            updateHistory();
        }

        function updateHistory() {
            const historyList = document.getElementById('historyList');
            historyList.innerHTML = '';

            history.forEach((record, index) => {
                const li = document.createElement('li');
                li.innerHTML = `<a href="#" onclick="expandRecord(${index})">${record.time}</a>`;
                historyList.appendChild(li);
            });
        }

        function expandRecord(index) {
            const record = history[index];
            alert(`时间: ${record.time}\n输入: ${JSON.stringify(record.inputs)}\n结果: ${record.result.toFixed(2)} 吨CO2`);
        }

        function clearHistory() {
            history = [];
            updateHistory();
        }

        function saveHistory() {
            const headers = ["时间", "工艺排放 (万吨)", "能源消耗 (千)", "资源消耗 (吨)", "工艺排放因子", "能源消耗因子", "资源消耗因子", "总碳排放量 (吨)"];
            const rows = history.map(record => [
                record.time,
                record.inputs.processEmission,
                record.inputs.energyConsumption,
                record.inputs.resourceConsumption,
                record.inputs.processFactor,
                record.inputs.energyFactor,
                record.inputs.resourceFactor,
                record.result.toFixed(2)
            ]);

            // 创建 CSV 内容
            const csvContent = headers.join(",") + "\n" + rows.map(row => row.join(",")).join("\n");

            // 创建 Blob 对象，指定 MIME 类型和编码
            const blob = new Blob(["\uFEFF" + csvContent], { type: "text/csv;charset=utf-8;" });

            // 创建下载链接
            const link = document.createElement("a");
            link.href = URL.createObjectURL(blob);
            link.download = "carbon_history.csv";
            document.body.appendChild(link);
            link.click();
            document.body.removeChild(link);
        }
    </script>
</body>
</html>